package com.jsle.financial.manager;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.jsle.financial.dao.UserDao;
import com.jsle.financial.domain.User;
import com.jsle.financial.domain.User_UserGroups;
import com.jsle.financial.util.Constants;
import com.jsle.financial.vo.Result;

/**
 * Manager 用于事物控制
 * @author zhangyanqing
 * @date 2012-10-27
 */
public class UserManagerImpl implements UserManager{
	private Log log = LogFactory.getLog(UserManagerImpl.class);
	
	@Resource
	private UserDao userDao;
	
	/**
	 * 新增一用户
	 * @param user
	 * @return
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor=Exception.class)
	public Result addUser(User user){
		Result result = new Result();
		try {
			result = userDao.addUser(user);
		} catch (Exception e) {
			result.setResultCode(Constants.Manager.FC_RESULT_INSERT_TRANSACTIONAL_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Manager.FC_RESULT_INSERT_TRANSACTIONAL_ERROR_MESSAGE, "User"));
			log.error("new user error## "+ e.getMessage());
		}
		return result;
	}
	/**
	 * 删除一用户
	 * @param user
	 * @return
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor=Exception.class)
	public Result delUsers(User user){
		Result result = new Result();
		try {
			result = userDao.delUsers(user);
		} catch (Exception e) {
			result.setResultCode(Constants.Manager.FC_RESULT_DEL_TRANSACTIONAL_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Manager.FC_RESULT_DEL_TRANSACTIONAL_ERROR_MESSAGE, "User"));
			log.error("del user error## "+ e.getMessage());
		}
		return result;
	}
	/**
	 * 更新一用户信息
	 * @param user
	 * @return
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor=Exception.class)
	public Result updateUser(User user){
		Result result = new Result();
		try {
			result = userDao.updateUser(user);

		} catch (Exception e) {
			result.setResultCode(Constants.Manager.FC_RESULT_UPDATE_TRANSACTIONAL_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Manager.FC_RESULT_UPDATE_TRANSACTIONAL_ERROR_MESSAGE, "User"));
			log.error("update user error## "+ e.getMessage());
		}
		return result;
	}
	
	/**
	 * 用户配置用户组
	 * @param user
	 * @return
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor=Exception.class)
	public Result addUserAndUserGroups(User_UserGroups uug){
		Result result = new Result();
		try {
			result = userDao.addUserAndUserGroups(uug);
		} catch (Exception e) {
			result.setResultCode(Constants.Manager.FC_RESULT_INSERT_TRANSACTIONAL_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Manager.FC_RESULT_INSERT_TRANSACTIONAL_ERROR_MESSAGE, "User"));
			log.error("new User_UserGroups error## "+ e.getMessage());
		}
		return result;
	}
	
}
